package de.codi.wrapper;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import de.unima.alcomox.exceptions.AlcomoException;

import eu.sealsproject.platform.res.domain.omt.IOntologyMatchingToolBridge;
import eu.sealsproject.platform.res.tool.api.ToolBridgeException;
import eu.sealsproject.platform.res.tool.api.ToolException;
import eu.sealsproject.platform.res.tool.api.ToolType;
import eu.sealsproject.platform.res.tool.impl.AbstractPlugin;

public class MatcherBridge extends AbstractPlugin implements IOntologyMatchingToolBridge {

	@Override
	public URL align(URL source, URL target) throws ToolBridgeException, ToolException {
		Main codi;
		try {
			codi = new Main();
			try {
				URI uri = codi.align(source.toURI(), target.toURI());				
				return new URL(uri.toURL().toString());
			}
			catch (URISyntaxException e1) {
				throw new ToolBridgeException("cannot convert the input param to URI as required");
			}
			catch (AlcomoException e) {
				throw new ToolBridgeException("FAIL! ALCOMO!", e);
			} catch (MalformedURLException e) {
				throw new ToolBridgeException("FAIL! MalFormedURL!", e);
			} catch (IOException e) {
				throw new ToolBridgeException("FAIL! IOException!", e);
			}
		}
		catch (NumberFormatException numberFormatE) {
			throw new ToolBridgeException("cannot correctly read from configuration file", numberFormatE);
		}			
	}

	/**
	* This functionality is not supported by the tool. In case
	* it is invoced a ToolException is thrown.
	*/
	public URL align(URL source, URL target, URL inputAlignment) throws ToolBridgeException, ToolException {
		throw new ToolException("functionality of called method is not supported");
	}

	/**
	* In our case the DemoMatcher can be executed on the fly. In case
	* prerequesites are required it can be checked here. 
	*/
	public boolean canExecute() {
		return true;
	}

	/**
	* The DemoMatcher is an ontology matching tool. SEALS supports the
	* evaluation of different tool types like e.g., reasoner and storage systems.
	*/
	public ToolType getType() {
		return ToolType.OntologyMatchingTool;
	}
}